import type {
  PlatformPageTypeValue,
  PlatformTypeValue,
  ThemeType,
} from '../../../../types/interfaces'
import {
  loggerDeduplicator,
  PAGE_ELEMENTS,
} from '../../../../types/constants'
import { PageHandler } from '../../../page_handler'
import { UNDEFINED_PAGE_TYPE } from '../../../page_handler/type'
import { BaseThemeService } from './base'

export abstract class SearchThemeService extends BaseThemeService {
  protected declare elements: any
  abstract wrapperSelector: string // 最外层增加包裹，提高特异性
  abstract name: string
  abstract platformPageType: PlatformPageTypeValue

  protected constructor(platform: PlatformTypeValue) {
    super(platform)
    this.elements = PAGE_ELEMENTS[platform]
  }

  getThemeStyles(theme: ThemeType): string {
    if (!this.elements) {
      loggerDeduplicator.error(`[${this.name}] 页面元素配置为空，无法获取主题样式`)
      return ''
    }

    const currentPageType = PageHandler.getCurrentPageType()
    if (!currentPageType || currentPageType === UNDEFINED_PAGE_TYPE) {
      loggerDeduplicator.error(`[${this.name}] 未识别到正确的页面类型，无法获取主题样式`)
      return ''
    }

    const styles = `/* ${theme} theme */
${this.getStyleVar(theme)}
${this.getPageStyles(theme)}
${this.wrapperSelector} {
  ${this.getBaseStyles(theme)}
  ${this.getMainStyles(theme)}
  ${this.getPatchedStyles(theme)}
  ${this.getFixedStyles(theme)}
}
`
    return this.normalizeCssIndentation(styles)
  }

  getBaseStyles(_theme: ThemeType): string {
    return ``
  }

  protected getMainStyles(_theme: ThemeType): string {
    return ''
  }

  protected getPageStyles(_theme: ThemeType): string {
    return ''
  }

  protected getPatchedStyles(_theme: ThemeType): string {
    return ''
  }

  protected getFixedStyles(_theme: ThemeType): string {
    return ''
  }
}
